<?php

namespace auth;

use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Auth
{

    // 认证方法
    static public function authenticate($u_name, $u_pwd, $captcha)
    {
        $result = self::vcodeCheck($captcha);
        if (!$result) {
            return ['code' => 1001, 'msg' => '验证码不正确'];
        }
        $auth_info = Db::name(config("auth.USER_TABLE"))->where("u_name", $u_name)->find();
        if ($auth_info['status'] != 1){
            return ['code' => 1002, 'msg' => '此账号被禁用'];
        }
        if (!$auth_info) {
            return ['code' => 1002, 'msg' => '用户名或者密码错误'];
        }
        if ($auth_info['u_pwd'] != md5($u_pwd)) {
            return ['code' => 1003, 'msg' => '用户名或者密码错误'];
        }
        session(config("auth.USER_AUTH_KEY"), $auth_info['id']);
        session(config('auth.USER_AUTH_NAME'), $auth_info['u_name']);
        if ($auth_info['id'] == 1 || $auth_info['id'] == 2 || $auth_info['id'] == 3) session(config("auth.ADMIN_AUTH_KEY"), true);
        return ['code' => 0, 'msg' => '登录成功'];
    }

    static public function checkAuth()
    {
        $admin_id = session(config("auth.USER_AUTH_KEY"));
        if ($admin_id == 1 || $admin_id == 2 || $admin_id == 3) { //超级管理员不验证权限
            return true;
        }
        $NOT_AUTH_MODULE = strtolower(config("auth.NOT_AUTH_MODULE"));
        $NOT_AUTH_MODULE = explode(",", $NOT_AUTH_MODULE);
        if (in_array(strtolower(CONTROLLER), $NOT_AUTH_MODULE)) return true;
        $node_id = Db::name('auth_nodes')->where("url", CONTROLLER . "/" . ACTION)->value('id'); //获取节点ID
        if (!$node_id) {
            return false;
        }
        //当前管理员权限
        $rules = Db::name("auth_admin")->alias("a")->join("auth_group b", "a.group_id=b.group_id", "LEFT")->where("a.id", $admin_id)->value("b.rules");
        $rules = explode(',', $rules); //已授权节点
        if (!in_array($node_id, $rules)) {
            return false;
        }
        return true;
    }


    static public function leftMenu()
    {
        $admin_id = session(config("auth.USER_AUTH_KEY")); // 管理员ID
        $admin_left_menu = cache('admin_left_menu_userid_' . session(config("auth.USER_AUTH_KEY")));
        if ($admin_left_menu) {
            //return $admin_left_menu;
        }
        $rules = Db::name("auth_admin")
            ->alias("a")
            ->join("auth_group b", "a.group_id=b.group_id", "LEFT")
            ->where("a.id", $admin_id)
            ->value("b.rules");//已授权节点
        $rules = explode(",", $rules);//转换授权节点字符串为数组
        $nodesArray = Db::name("auth_nodes")->where("menustatus", 1)->order('sort')->select()->toArray(); //获取所有节点
        $nodes = array();
        foreach ($nodesArray as $key => $val) {
            $nodes[$val["id"]] = $val;
        }
        $menus = array();
        foreach ($nodes as $key => $val) {
            $nodes[$key]['url'] = (string)url(SYS_PATH . "/" . $val['url']);
            if ($val['pid'] != 0) continue; //如果不是一级菜单，跳出本次循环
            if (ADMIN_ID != 1 && ADMIN_ID != 2 && ADMIN_ID != 3) {
                if (in_array($val['id'], $rules)) $menus[] = $val;
            } else {
                $menus[] = $val;
            }
        }
        foreach ($menus as $k => $v) {
            foreach ($nodes as $kk => $vv) {
                if ($v['id'] != $vv['pid']) continue; //只显示二级菜单，三级直接忽略
                if (ADMIN_ID != 1 && ADMIN_ID != 2 && ADMIN_ID != 3) {
                    if (in_array($vv['id'], $rules)) $menus[$k]['children'][] = $vv;
                } else {
                    $menus[$k]['children'][] = $vv;
                }
            }
        }
        cache('admin_left_menu_userid_' . session(config("auth.USER_AUTH_KEY")), $menus);
        return $menus;
    }


    /**
     * 验证码输入验证
     * @author 四川挚梦*科技有限*公司
     * @date 2018-03-18
     */
    static public function vcodeCheck($captcha)
    {
        return captcha_check($captcha);
    }

}
